package com.naiterui.ehp.bs.search.service.impl.core;

import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bs.search.exception.ExceptionCodes;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @author guoyongxiang
 * @date 2019-08-27 15:21
 * @since 1.0.0
 */
@Service
public class EsSearchService {

    private static final Logger LOGGER = LoggerFactory.getLogger(EsSearchService.class);
    /**
     * 搜索服务注入
     */
    @Autowired
    private RestHighLevelClient esClient;

    public SearchResponse search(SearchRequest searchRequest) throws BusinessException {
        try {
            return esClient.search(searchRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            LOGGER.error("搜索请求异常 searchRequest:{}，e: {}", searchRequest, e);
            throw new BusinessException(ExceptionCodes.SEARCH_OPERATION_FAILED);
        }
    }
}
